//
//  HP_InstalmentViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/8/30.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "HP_InstalmentViewController.h"
#import "HP_ThwartwiseTypeViewCell.h"
#import "HP_ClassifyTitleCollectionViewCell.h"
#import "HP_GoodsCollectionViewCell.h"
#import "HP_SectionModel.h"
#import "HP_CellModel.h"
#import "HP_HeaderCollectionViewCell.h"

@interface HP_InstalmentViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *myCollectionView;
@property(nonatomic,strong)NSMutableArray<HP_SectionModel*>*dataArray;
@property(nonatomic,strong)UIView *headerView;

@end

@implementation HP_InstalmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeComponent];
    [self bindFirstPageData:[[NSDictionary alloc]init]];
    
    
}

#pragma mark ------绑数据---------------
-(void)bindFirstPageData:(NSDictionary *)data{
    [self.dataArray removeAllObjects];
    
    HP_CellModel *headerModel = [self creatCellModel:[HP_HeaderCollectionViewCell class] userInfo:@"" size:[HP_HeaderCollectionViewCell calculateSize:@""]];
    HP_SectionModel *sec = [self createSectionModel:@[headerModel] headerHeight:0 footerHeight:0];
    [self.dataArray addObject:sec];
    
    
    HP_CellModel *typeModel = [self creatCellModel:[HP_ThwartwiseTypeViewCell class] userInfo:@"" size:[HP_ThwartwiseTypeViewCell calculateSize:@""]];
    HP_SectionModel *section0 = [self createSectionModel:@[typeModel] headerHeight:0 footerHeight:0];
    [self.dataArray addObject:section0];
    
    
    HP_CellModel *titleModel = [self creatCellModel:[HP_ClassifyTitleCollectionViewCell class] userInfo:@"3C推荐" size:[HP_ClassifyTitleCollectionViewCell calculateSize:@""]];
    HP_SectionModel *section1 = [self createSectionModel:@[titleModel] headerHeight:0 footerHeight:0];
    [self.dataArray addObject:section1];
    
    
    NSMutableArray *temp = [[NSMutableArray alloc]init];
    for (int i=0; i<4; i++) {
        HP_CellModel *goodCell = [self creatCellModel:[HP_GoodsCollectionViewCell class] userInfo:@"" size:[HP_GoodsCollectionViewCell calculateSize:@""]];
        [temp addObject:goodCell];
        
    }
     HP_SectionModel *section2 = [self createSectionModel:temp headerHeight:0 footerHeight:0];
    [self.dataArray addObject:section2];
    
    HP_CellModel *titleModel2 = [self creatCellModel:[HP_ClassifyTitleCollectionViewCell class] userInfo:@"3E推荐" size:[HP_ClassifyTitleCollectionViewCell calculateSize:@""]];
    HP_SectionModel *section3 = [self createSectionModel:@[titleModel2] headerHeight:0 footerHeight:0];
    [self.dataArray addObject:section3];
    NSMutableArray *tempArr = [[NSMutableArray alloc]init];
    for (int i=0; i<4; i++) {
        HP_CellModel *goodCell = [self creatCellModel:[HP_GoodsCollectionViewCell class] userInfo:@"" size:[HP_GoodsCollectionViewCell calculateSize:@""]];
        [tempArr addObject:goodCell];
        
    }
    HP_SectionModel *section4 = [self createSectionModel:temp headerHeight:0 footerHeight:0];
    [self.dataArray addObject:section4];
    
    
    
    for (HP_SectionModel *sectionModel in self.dataArray) {
        for (HP_CellModel *cellModel in sectionModel.cells) {
            [_myCollectionView registerClass:NSClassFromString(cellModel.className) forCellWithReuseIdentifier:cellModel.reuseIdentifier];
        }
    }
    
    [_myCollectionView reloadData];
    
}


-(void)initializeComponent{
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _myCollectionView.frame = self.view.frame;
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    _myCollectionView.backgroundColor = ListMaoBackgroundColor;
    _myCollectionView.showsVerticalScrollIndicator = NO;
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerID"];
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerID"];
    self.myCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 134, 0);
    [self.view addSubview:_myCollectionView];
    
}

#pragma mark -----------collectionViewDelegate--------------

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.dataArray.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    HP_SectionModel *s = self.dataArray[section];
    if (s.cells) {
        return s.cells.count;
    }
    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count != 0) {
        HP_SectionModel *s = _dataArray[indexPath.section];
        HP_CellModel *item = s.cells[indexPath.row];
        
        
        if ([item.className isEqualToString:NSStringFromClass([HP_HeaderCollectionViewCell class])]) {
            HP_HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:item.reuseIdentifier forIndexPath:indexPath];
            //此处根据用户是否有订单去 刷新
            [cell updateUIwithStatue:YES];
            SEL selector = NSSelectorFromString(@"fillData:");
            if([cell respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
                [cell performSelector:selector withObject:item.userInfo];
#pragma clang diagnostic pop
            }
            
            return cell;
        }else{
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:item.reuseIdentifier forIndexPath:indexPath];
            SEL selector = NSSelectorFromString(@"fillData:");
            if([cell respondsToSelector:selector]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
                [cell performSelector:selector withObject:item.userInfo];
#pragma clang diagnostic pop
            }
            
            return cell;
        }
        
    }
    return nil;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HP_SectionModel *s = _dataArray[indexPath.section];
    HP_CellModel *item = s.cells[indexPath.row];
    return item.size;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 3 || section == 5) {
        return UIEdgeInsetsMake(0, 11 ,0, 11);
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
    
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    if (section == 3 || section == 5) {
        return 8;
    }
    return 0;
    
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 3) {
        return 8;
    }
    return 0;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *view = nil;
    
    if (kind == UICollectionElementKindSectionFooter) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerID" forIndexPath:indexPath];
        view.backgroundColor = RGBCOLOR(242, 242, 242,1);
        
        
        
        
    }else{
        
        
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerID" forIndexPath:indexPath];
        view.backgroundColor = RGBCOLOR(242, 242, 242,1);
        
        
    }
    
    return view;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(ScreenWidthMao, 0);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    return CGSizeMake(ScreenWidthMao, 0);
}




-(HP_CellModel *)creatCellModel:(Class)cls userInfo:(id)userInfo size:(CGSize)size{
    HP_CellModel *model = [[HP_CellModel alloc]init];
    model.selectorString = UITableViewCellSelectionStyleNone;
    model.userInfo = userInfo;
    model.size = size;
    model.className = NSStringFromClass(cls);
    return model;
}

-(HP_SectionModel*)createSectionModel:(NSArray<HP_CellModel*>*)items headerHeight:(CGFloat)headerHeight footerHeight:(CGFloat)footerHeight {
    HP_SectionModel *model = [HP_SectionModel sectionModelWithTitle:nil cells:items];
    model.headerhHeight = headerHeight;
    model.footerHeight = footerHeight;
    return model;
}
-(NSMutableArray<HP_SectionModel *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]init];
    }
    return _headerView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
